
//
// 110320 - AH - Created.
// 111002 - AH - Missing java doc.
//

package org.soekrat.engine.swnetwork;

/**
 * <p>
 *   {@link NodeMeanCalculator} that computes the geometric mean for a node.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class NodeGeometricMeanDoubleEdgesCalculator 
  implements NodeMeanCalculator 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public NodeGeometricMeanDoubleEdgesCalculator(){}

  // Specified in NodeMeanCalculator.
  public double calculate(SpareWheelNetwork network, SpareWheelNetworkNode node) 
  {
    double sum=0.0;
    
    SpareWheelNetworkEdge[] edges=network.getEdgesOf(node);
    
    int n=edges.length;
    int nm1=n-1;
    
    double k=0;
    
    for (int i=0; i<nm1; i++)
    {
      SpareWheelNetworkEdge a=edges[i];
      double wa=network.getWeightOfEdge(a);
      
      for (int j=i+1; j<n; j++)
      {
        SpareWheelNetworkEdge b=edges[j];
        double wb=network.getWeightOfEdge(b);
        
        double c=Math.sqrt(wa*wb);
        
        sum+=c;
        
        k=k+1;
      }
    }
    
    return sum/k;
  }

}
